package com.oneday.nuo.base.controller;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oneday.nuo.base.common.NuoUtil;
import com.oneday.nuo.base.common.RedisUtil;
import com.oneday.nuo.base.common.SpringUtil;
import com.oneday.nuo.base.config.CurrentUser;
import com.oneday.nuo.base.dto.NuoUserDTO;
import com.oneday.nuo.base.entity.NuoUserEntity;
import com.oneday.nuo.base.response.NuoResult;
import com.oneday.nuo.base.response.NuoSuccessStatus;
import com.oneday.nuo.base.security.user.NuoUser;
import com.oneday.nuo.base.service.NuoUserService;
import com.oneday.nuo.base.tactics.token.TokenContext;
import com.oneday.nuo.base.tactics.token.TokenTactics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 用户表(NuoUser)表控制层
 *
 * @author liushuai
 * @since 2021-03-13 23:37:48
 */
@RestController
@RequestMapping("api/user")
@Api(tags="NuoUserController")
@ApiModel(value = "",description = "用户接口")
public class NuoUserController {
    @Autowired
    private NuoUserService nuoUserService;
    @Value("${token.tokenType}")
    private String  tokenTactics;

    @ApiOperation(value = "用户注销" ,  notes="用户注销")
    @GetMapping("loginOut")
    public NuoResult loginOut(){

        TokenTactics tokenTacticsImpl = (TokenTactics) SpringUtil.getBean(tokenTactics);
        TokenContext tokenContext = new TokenContext(tokenTacticsImpl,CurrentUser.getCurrentUser());
        //用户退出登录
        tokenContext.exitLogin();
        return NuoResult.buildResponse(NuoSuccessStatus.LOGIN_OUT_SUCCESS);
    }

    @ApiOperation(value = "用户保存" ,  notes="用户保存")
    @PostMapping("saveUser")
    public NuoResult saveUser(@RequestBody NuoUserDTO nuoUserDTO){
        nuoUserService.saveUser(nuoUserDTO);
        return NuoResult.buildResponseSave();
    }
    @ApiOperation(value = "根据id查询用户" ,  notes="根据id查询用户")
    @GetMapping("queryUserById")
    public NuoResult<NuoUserDTO> queryUserById(@RequestParam String id){
        NuoUserDTO dto = nuoUserService.queryUserById(id);
        dto.setPassword(null);
        return NuoResult.buildResponseQuery(dto);
    }
    @ApiOperation(value = "根据id删除用户" ,  notes="根据id删除用户")
    @GetMapping("deleteUserById")
    public NuoResult deleteUserById(@RequestParam Long id){
        nuoUserService.deleteUserById(id);
        return NuoResult.buildResponseDelete();
    }
    @ApiOperation(value = "根据部门id分页查询用户列表" ,  notes="根据部门id分页查询用户列表")
    @GetMapping("queryUserPageByParam")
    public NuoResult<IPage<NuoUserDTO>> queryUserPageByParam( Page page,NuoUserDTO nuoUserDTO){
        IPage<NuoUserEntity> entityIPage =  nuoUserService.queryUserPageByParam(page,nuoUserDTO);
        IPage<NuoUserDTO> iPage = (IPage<NuoUserDTO>) NuoUtil.convertPage(NuoUserDTO.class, entityIPage);
        return NuoResult.buildResponseQuery(iPage);
    }




}
